package cn.oms.oms.service.impl;

import cn.oms.oms.entity.Department;
import cn.oms.oms.entity.Staff;
import cn.oms.oms.entity.User;
import cn.oms.oms.exception.ServiceException;
import cn.oms.oms.mapper.DepartmentMapper;
import cn.oms.oms.mapper.StaffMapper;
import cn.oms.oms.mapper.UserMapper;
import cn.oms.oms.service.IStaffService;
import cn.oms.oms.utils.TimeTool;
import cn.oms.oms.vo.StaffVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 根据查询条件查询员工列表
     * @param staffVo
     * @return
     */
    @Override
    public PageInfo<Staff> selectStaffList(StaffVo staffVo) {
        QueryWrapper<Staff> query = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(staffVo.getStaffName())){
            query.like("staffName",staffVo.getStaffName());
        }
        if (staffVo.getSex()!=null&&!"".equals(staffVo.getSex())){
            query.eq("sex",staffVo.getSex());
        }
        if (staffVo.getJobType()!=null&&!"".equals(staffVo.getJobType())){
            query.eq("jobType",staffVo.getJobType());
        }
        if (staffVo.getDepartmentId()!=null&&!"".equals(staffVo.getDepartmentId())){
            query.eq("departmentId",staffVo.getDepartmentId());
        }
        query.eq("deleteFlag",0);
        PageHelper.startPage(staffVo.getPageNum(),staffVo.getPageSize());
        List<Staff> staffList = staffMapper.selectList(query);

        for (Staff staff : staffList){
            QueryWrapper<Department> query1 = new QueryWrapper<>();
            query1.eq("deleteFlag",0);
            query1.eq("id",staff.getDepartmentId());
            Department department = departmentMapper.selectOne(query1);
            if (department!=null){
                staff.setDepartmentName(department.getDepartmentName());
            }

            if (staff.getJobType()==1){
                staff.setJobTypeName("缴费员");
            }
            if (staff.getJobType()==2){
                staff.setJobTypeName("护士");
            }
            if (staff.getJobType()==3){
                staff.setJobTypeName("医生");
            }
            if (staff.getJobType()==4){
                staff.setJobTypeName("药师");
            }
            if (staff.getJobType()==5){
                staff.setJobTypeName("管理员");
            }

            if (staff.getSex()==0){
                staff.setSexName("男");
            }
            if (staff.getSex()==1){
                staff.setSexName("女");
            }

            // 日期格式化
            String date = staff.getDateOfBirth();
            date = TimeTool.formattingTime2(date);
            staff.setReturnDateOfBirth(date);

        }

        return new PageInfo<>(staffList);
    }


    /**
     * 查询员工详情
     * @param staffVo
     * @return
     */
    @Override
    public Staff selectStaffDetail(StaffVo staffVo) {
        if (staffVo.getStaffId()==null||"".equals(staffVo.getStaffId())){
            throw new ServiceException("参数异常，员工ID为空");
        }
        QueryWrapper<Staff> query = new QueryWrapper<>();
        query.eq("staffId",staffVo.getStaffId());
        query.eq("deleteFlag",0);
        Staff staff = staffMapper.selectOne(query);
        if (staff==null){
            throw new ServiceException("参数异常,员工不存在");
        }
        if (staff.getJobType()!=null){
            if (staff.getJobType()==1){
                staff.setJobTypeName("缴费员");
            }
            if (staff.getJobType()==2){
                staff.setJobTypeName("护士");
            }
            if (staff.getJobType()==3){
                staff.setJobTypeName("医生");
            }
            if (staff.getJobType()==4){
                staff.setJobTypeName("药师");
            }
            if (staff.getJobType()==5){
                staff.setJobTypeName("管理员");
            }
        }

        //日期格式化
//        String date = TimeTool.formattingTime2(staff.getDateOfBirth());
//        staff.setReturnDateOfBirth(date);
        staff.setReturnDateOfBirth(staff.getDateOfBirth());

        // 科室格式化
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",staff.getDepartmentId());
        queryWrapper.eq("deleteFlag",0);
        Department department = departmentMapper.selectOne(queryWrapper);
        if (department!=null){
            staff.setDepartmentName(department.getDepartmentName());
        }

        // 性别格式化
        if (staff.getSex()==0){
            staff.setSexName("男");
        }
        if (staff.getSex()==1){
            staff.setSexName("女");
        }

        return staff;
    }


    /**
     * 新增员工
     * @param staffVo
     * @return
     */
    @Override
    public String addStaff(StaffVo staffVo) {
        Staff staff = new Staff();

        //判断用户ID是否为空
        if (staffVo.getUserId()==null||"".equals(staffVo.getUserId())){
            throw new ServiceException("参数异常，用户ID不能为空");
        }
        staff.setUserId(staffVo.getUserId());

        // 判断用户是否被绑定
        QueryWrapper<Staff> query = new QueryWrapper<>();
        query.eq("userId", staffVo.getUserId());
        query.eq("deleteFlag",0);
        Staff staff1 = staffMapper.selectOne(query);
        if (staff1!=null){
            throw new ServiceException("用户已绑定员工信息,请重新绑定");
        }

        // 员工姓名
        if (StringUtil.isEmpty(staffVo.getStaffName())){
            throw new ServiceException("参数异常，员工姓名不能为空");
        }
        staff.setStaffName(staffVo.getStaffName());

        // 性别
        if (staffVo.getSex()==null||"".equals(staffVo.getSex())){
            throw new ServiceException("参数异常，性别不能为空");
        }
        staff.setSex(staffVo.getSex());

        staff.setDateOfBirth(staffVo.getDateOfBirth());

        if (StringUtil.isEmpty(staffVo.getJobTitleName())){
            throw new ServiceException("参数异常，职称名称不能为空");
        }
        staff.setJobTitleName(staffVo.getJobTitleName());

        if (staffVo.getDepartmentId()==null||"".equals(staffVo.getDepartmentId())){
            throw new ServiceException("参数异常，科室ID不能为空");
        }
        staff.setDepartmentId(staffVo.getDepartmentId());

        if (staffVo.getJobType()==null||"".equals(staffVo.getJobType())){
            throw new ServiceException("参数异常，工作人员类别不能为空");
        }
        staff.setJobType(staffVo.getJobType());

        staff.setDeleteFlag(0);

        int num = staffMapper.insert(staff);

        if (num!=1){
            throw new ServiceException("数据库新增异常！！！");
        }

        return "ok";
    }

    /**
     * 修改员工
     * @param staffVo
     * @return
     */
    @Override
    public String updateStaff(StaffVo staffVo) {
        if (staffVo.getStaffId()==null||"".equals(staffVo.getStaffId())){
            throw new ServiceException("参数异常，未知员工ID");
        }
        QueryWrapper<Staff> query = new QueryWrapper<>();
        query.eq("staffId",staffVo.getStaffId());
        query.eq("deleteFlag",0);
        Staff staff = staffMapper.selectOne(query);
        if (staff==null){
            throw new ServiceException("员工不存在");
        }
        // 参数设置
        if (staffVo.getSex()!=null&&!"".equals(staffVo.getSex())){
            staff.setSex(staffVo.getSex());
        }
        if (StringUtil.isNotEmpty(staffVo.getDateOfBirth())){
            staff.setDateOfBirth(staffVo.getDateOfBirth());
        }
        if (StringUtil.isNotEmpty(staffVo.getJobTitleName())){
            staff.setJobTitleName(staffVo.getJobTitleName());
        }
        if (staffVo.getDepartmentId()!=null&&!"".equals(staffVo.getDepartmentId())){
            staff.setDepartmentId(staffVo.getDepartmentId());
        }
        if (staffVo.getJobType()!=null&&!"".equals(staffVo.getJobType())){
            staff.setJobType(staffVo.getJobType());
        }

        int num = staffMapper.updateById(staff);

        if (num!=1){
            throw new ServiceException("数据库修改异常！！！");
        }

        return "ok";
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @Override
    public String deleteStaff(Integer id) {
        if (id==null||"".equals(id)){
            throw new ServiceException("参数异常，员工ID为空");
        }
        QueryWrapper<Staff> query = new QueryWrapper<>();
        query.eq("staffId",id);
        query.eq("deleteFlag",0);
        Staff staff = staffMapper.selectOne(query);
        if (staff==null){
            throw new ServiceException("员工不存在");
        }
        staff.setDeleteFlag(1);
        int num = staffMapper.updateById(staff);

        if (num!=1){
            throw new ServiceException("数据库删除异常！！！");
        }

        return "ok";
    }
}
